package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.ProductModel;
import com.zeng.jiudian.bean.ProductModelExample;
import com.zeng.jiudian.dao.ProductDao;
import com.zeng.jiudian.mapper.ProductMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/23 - 15:46
 */
public class ProductDaoImpl implements ProductDao
{
    private SqlSession sqlSession;

    @Override
    public int add(ProductModel productModel)
    {

        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        productModel.setCreateTime(dateFormat.format(new Date()));
        productModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            count = mapper.insert(productModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel)
    {
        List<ProductModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            list = mapper.selectAll(productModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int delById(int id)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public ProductModel findById(int id)
    {
        ProductModel productModel = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            productModel = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return productModel;
    }

    @Override
    public int update(ProductModel productModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        productModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            count = mapper.updateByPrimaryKeySelective(productModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int getCount(ProductModel productModel)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            count = mapper.selectCount(productModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ProductModel> getAll()
    {

        List<ProductModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            ProductModelExample example = new ProductModelExample();
            example.setStartRow(0);
            example.setPageSize(6);
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }
}
